<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <!-- 如果时一级子部门 给函数 hAdd传一个空的pid 就是一级部门 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree :default-expand-all="true" :data="list" :props="{children:'children',label:'name'}">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 点击弹出子组件 -->
      <el-dialog
        v-if="showDialog"
        :title="isEdit?'编辑部门':'新增部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <addorEdit
          v-if="showDialog"
          :id="curId"
          :is-edit="isEdit"
          :origin-list="originList"
          @close="showDialog= false"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import addorEdit from './deptdialog.vue'
import { getDepartment, deleteDepartment } from '@/api/departments'
import { transDataToTreeData } from '@/utils/index'
export default {
  // 注册组件
  components: {
    addorEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [], // 存储数据列表
      showDialog: false, // 弹层的关闭和显示
      curId: '', // 存储当前点击部门的id
      isEdit: false, // true新增 ||添加 false
      originList: [] // 给子组件的有效数据
    }
  },
  created() {
    // 页面打开就直接渲染直接渲染
    this.loadDepartment()
  },
  methods: {
    // 删除部门
    async hDel(id) {
      try {
        const res = await deleteDepartment(id)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.loadDepartment()
      } catch (error) {
        console.log(error)
      }
    },
    // 关闭弹层 重新渲染数据
    hSuccess() {
      this.showDialog = false
      this.loadDepartment()
    },
    // 实现编辑点击之后将点击时候的id传给子
    hEdit(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = true
    },
    // 实现新增点击之后将点击时候的id传给子
    hAdd(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = false
    },
    // 获取部门列表
    async  loadDepartment() {
      try {
        const res = await getDepartment()
        // console.log(res)
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        // 第一个数据不需要  直接删除
        res.data.depts.shift()
        this.list = transDataToTreeData(res.data.depts)
        // this.list = res.data.depts
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
